/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.user.student;

import hibernate.entity.Accounts;
import hibernate.entity.Courses;
import hibernate.entity.Staffs;
import hibernate.entity.Students;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import ui.common.AbstractUIBean;
import ui.user.mail.MailComposer;
import util.Converter;
import util.FileUpload;

/**
 *
 * @author Administrator
 */
@ManagedBean
@RequestScoped
public class StudentBean extends AbstractUIBean {

    /**
     * Creates a new instance of StudentBean
     */
    Accounts acc = getCurrentAccount();
    String fullName;
    String userName;
    String email;
    String image;
    String address;
    Date birthday;
    boolean gender;

    public StudentBean() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        Students st = (Students) session.getAttribute("currStudent");
        if (st != null) {
            acc = st.getAccounts();
            fullName = acc.getFullName();
            userName = acc.getUserName();
            email = acc.getEmail();
            address = acc.getAddress();
            birthday = acc.getBirthday();
            image = acc.getImage();
        }

    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public boolean isGender() {
        return gender;
    }

    public void setGender(boolean gender) {
        this.gender = gender;
    }

    public String getUserName() {

        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public Staffs getCurrStaff() {
        return getCurrentAccount().getStaffses().iterator().next();
    }

    public String doupdate() {
        try {
            HttpSession session = getHttpSession();
            String path = String.valueOf(session.getAttribute("path"));
            hbTransaction = hbSession.beginTransaction();
            acc.setAddress(address);
            acc.setBirthday(birthday);
            acc.setEmail(email);
            acc.setFullName(fullName);
            acc.setGender(gender);
            if (session.getAttribute("path") != null) {
                path = String.valueOf(session.getAttribute("path"));
                acc.setImage(path);
            }
            acc = (Accounts) hbSession.merge(acc);
            hbSession.update(acc);
            hbTransaction.commit();
            return "studentprofile";
        } catch (Exception e) {
            hbTransaction.rollback();
        }
        return null;
    }

    public void upload(FileUploadEvent event) {
        InputStream is = null;
        try {

            UploadedFile uploadFile = event.getFile();
            is = uploadFile.getInputstream();
            String absPath = uploadFile.getFileName();
            String path = new FileUpload().processFile(uploadFile);
            System.out.println("Done!");
            FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            HttpSession session = getHttpSession();
            session.setAttribute("path", path);
        } catch (IOException ex) {
            Logger.getLogger(MailComposer.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                is.close();
            } catch (IOException ex) {
                Logger.getLogger(MailComposer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public String staffView() {
        List<Staffs> listS = new ArrayList<Staffs>();
        List<Courses> listC = Converter.iteratorToList(getCurrentStudent().getCourseses().iterator());
        for (Courses courses : listC) {
            List<Staffs> listSt = Converter.iteratorToList(courses.getStaffses().iterator());
        }
        HttpSession session = getHttpSession();
        session.setAttribute("listStaff", "listS");
        return "staffview";
        
    }
}
